package com.nicy.controller;

import com.nicy.model.Student;
import com.nicy.utils.exception.BusinessRuntimeException;
import com.nicy.utils.exception.ResultCode;
import com.nicy.utils.vo.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Slf4j
public class StudentController {

    @RequestMapping(path = "/student",method = RequestMethod.POST)
    public void addStudent(@Valid @RequestBody Student student, BindingResult bindingResult){
        log.info("add new student:{}",student.toString());
        if (bindingResult.hasErrors()){

            List<ObjectError> list = bindingResult.getAllErrors();
            for (int i=0;i<list.size();i++){
               log.info(list.get(i).getDefaultMessage());
            }
            //throw new BusinessRuntimeException(ResultCode.USERNAME_ERROR);
            //log.info(String.valueOf(bindingResult.getFieldError("name").getDefaultMessage()));
        }

    }
}
